package com.linzhaowei.headhuntingservice;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.linzhaowei.headhuntingservice.adapter.JobInformationAdapter;
import com.linzhaowei.headhuntingservice.adapter.JobInformationSimpleAdapter;
import com.linzhaowei.headhuntingservice.adapter.JobObjectiveSimpleAdapter;
import com.linzhaowei.headhuntingservice.bean.JobInformation;
import com.linzhaowei.headhuntingservice.bean.JobObjective;
import com.linzhaowei.headhuntingservice.bean.User;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JobInformationManage extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private Button button;


    private List<JobInformation> jobInformationList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_information_manage);

        initViews();
        setEvents();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getJobInformation();
    }

    private void initViews() {
        recyclerView = findViewById(R.id.job_information_manage);
        button = findViewById(R.id.j_i_m_btn);
    }

    private void setEvents() {
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.j_i_m_btn:
                Intent intent = new Intent(JobInformationManage.this, JobInformationAdd.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void getJobInformation() {
        new Thread(() -> {
            HttpUtils httpUtils = new HttpUtils();
            try {
                SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
                String result = httpUtils.get(Ip.ip + "/user/job_information_by_user?user_id=" + String.valueOf(sharedPreferences.getInt("id", -1)));
                if(result.equals("")){
                    return;
                }
                JSONArray jsonArray = JSON.parseArray(result);
                jobInformationList=new ArrayList<>();
                for (int i = 0; i < jsonArray.size(); i++) {
                    jobInformationList.add(jsonArray
                            .getJSONObject(i)
                            .toJavaObject(JobInformation.class));
                }

                runOnUiThread(this::show);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    private void show() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        JobInformationSimpleAdapter jobInformationSimpleAdapter = new JobInformationSimpleAdapter(jobInformationList, this);
        recyclerView.setAdapter(jobInformationSimpleAdapter);
    }

}
